package Friday.th3;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class Main{
    public static void main(String[] args) {
        computer cpu=new computer();
    }
}

public class computer extends JFrame implements ActionListener {
    JMenuBar jmb;//菜单条
    JMenu jm[];//菜单
    JTextField jtf;//文本框
    JButton jb[];//按钮
    JPanel jp;//按钮面板
    String str[];//按钮名称
    private String saved_area;//存储区
    computer(){
        saved_area=new String("0");//初始化存储区
        jmb=new JMenuBar();//创建菜单条
        jm=new JMenu[3];//声明菜单
        jm[0]=new JMenu("查看(V)");//创建菜单
        jm[1]=new JMenu("编辑(E)");
        jm[2]=new JMenu("帮助(H)");
        //放置菜单
        for (int i=0;i<3;i++){
            jmb.add(jm[i]);
        }
        this.setJMenuBar(jmb);//放置菜单条
        jtf=new JTextField("0",30);//创建文本框
        jtf.setHorizontalAlignment(JTextField.RIGHT);//右对齐
        this.setLayout(new BorderLayout());//布局格式
        this.add(jtf,BorderLayout.NORTH);//放置文本框
        jp=new JPanel();//创建面板
        add(jp,BorderLayout.CENTER);//放置面板
        GridBagLayout gridBagLayout=new GridBagLayout();//网格包布局管理对象
        GridBagConstraints gridBagConstraints=new GridBagConstraints();//约束对象
        jp.setLayout(gridBagLayout);//面板网格包布局
        jb=new JButton[28];//创建按钮引用变量
        //设置按钮名称
        str=new String[]{new String("MC"),new String("MR"),new String("MS"),
                new String("M+"),new String("M-"),new String("<-(unvailable)"),
                new String("CE"),new String("C"),new String("±"),
                new String("√"),new String("7"),new String("8"),
                new String("9"),new String("/"),new String("%"),
                new String("4"),new String("5"),new String("6"),
                new String("*"),new String("1/x"),new String("1"),
                new String("2"),new String("3"),new String("-"),
                new String("="),new String("0"),new String("."),new String("+")};
        //为组件适应面板大小设置约束条件
        gridBagConstraints.weightx=1;
        gridBagConstraints.weighty=1;
        gridBagConstraints.fill=GridBagConstraints.BOTH;
        //在面板中安放按钮
        for(int i=0;i<28;i++){
            //按钮在面板布局中的坐标
            gridBagConstraints.gridy=i/5;
            gridBagConstraints.gridx=i%5;
            //i==26安放按钮"0"，宽度占两个单元，所以之后的按钮的x值都加一
            if(i>=26)gridBagConstraints.gridx++;
            int tx=gridBagConstraints.gridx;
            int ty=gridBagConstraints.gridy;
            //设置组件之间的间距
            gridBagConstraints.insets=new Insets(3,3,3,3);
            if(tx==0)gridBagConstraints.insets=new Insets(3,10,3,3);
            if(tx==4)gridBagConstraints.insets=new Insets(3,3,3,10);
            if(ty==0)gridBagConstraints.insets=new Insets(10,3,3,3);
            if(ty==5)gridBagConstraints.insets=new Insets(3,3,10,3);
            if(tx==0&&ty==0)gridBagConstraints.insets=new Insets(10,10,3,3);
            if(tx==4&&ty==0)gridBagConstraints.insets=new Insets(10,3,3,10);
            if(tx==0&&ty==5)gridBagConstraints.insets=new Insets(3,10,10,3);
            if(tx==4&&ty==4)gridBagConstraints.insets=new Insets(3,3,10,10);
            //创建按钮
            jb[i]=new JButton(str[i]);
            //这只按钮宽度和高度
            gridBagConstraints.gridwidth=1;//先处理默认值
            gridBagConstraints.gridheight=1;
            if(i==24)gridBagConstraints.gridheight=2;//按钮"="
            if(i==25)gridBagConstraints.gridwidth=2;//按钮"0"
            gridBagLayout.setConstraints(jb[i],gridBagConstraints);//为此按钮绑定约束条件
            jb[i].addActionListener(this);//为按钮注册监视器
            jp.add(jb[i]);//安放按钮
        }
        //设置窗体属性
        setTitle("计算器");
        this.setSize(500,500);
        this.setLocation(300,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    }
    @Override
    //实现事件响应处理的方法
    public void actionPerformed(ActionEvent actionEvent) {
        if (jb[25].equals(actionEvent.getSource())) {//"0"
            if(jtf.getText().equals("0")){//先判断原文本框是否是默认的0，如果是用按钮值替换掉
                jtf.setText("0");
            }
            else {
                jtf.setText(jtf.getText()+"0");//如果不是，将按钮值加到表达式末尾
            }
        }
        else if(jb[20].equals(actionEvent.getSource())){//"1"
            if(jtf.getText().equals("0")){
                jtf.setText("1");
            }
            else {
                jtf.setText(jtf.getText()+"1");
            }
        }
        else if(jb[21].equals(actionEvent.getSource())){//"2"
            if(jtf.getText().equals("0")){
                jtf.setText("2");
            }
            else {
                jtf.setText(jtf.getText()+"2");
            }
        }
        else if(jb[22].equals(actionEvent.getSource())){//"3"
            if(jtf.getText().equals("0")){
                jtf.setText("3");
            }
            else {
                jtf.setText(jtf.getText()+"3");
            }
        }
        else if(jb[15].equals(actionEvent.getSource())){//"4"
            if(jtf.getText().equals("0")){
                jtf.setText("4");
            }
            else {
                jtf.setText(jtf.getText()+"4");
            }
        }
        else if(jb[16].equals(actionEvent.getSource())){//"5"
            if(jtf.getText().equals("0")){
                jtf.setText("5");
            }
            else {
                jtf.setText(jtf.getText()+"5");
            }
        }
        else if(jb[17].equals(actionEvent.getSource())){//"6"
            if(jtf.getText().equals("0")){
                jtf.setText("6");
            }
            else {
                jtf.setText(jtf.getText()+"6");
            }
        }
        else if(jb[10].equals(actionEvent.getSource())){//"7"
            if(jtf.getText().equals("0")){
                jtf.setText("7");
            }
            else {
                jtf.setText(jtf.getText()+"7");
            }
        }
        else if(jb[11].equals(actionEvent.getSource())){//"8"
            if(jtf.getText().equals("0")){
                jtf.setText("8");
            }
            else {
                jtf.setText(jtf.getText()+"8");
            }
        }
        else if(jb[12].equals(actionEvent.getSource())){//"9"
            if(jtf.getText().equals("0")){
                jtf.setText("9");
            }
            else {
                jtf.setText(jtf.getText()+"9");
            }
        }
        else if(jb[26].equals(actionEvent.getSource())){//"."
            jtf.setText(jtf.getText()+".");
        }
        else if(jb[27].equals(actionEvent.getSource())){//"+"
            jtf.setText(jtf.getText()+"+");
        }
        else if(jb[23].equals(actionEvent.getSource())){//"-"
            jtf.setText(jtf.getText()+"-");
        }
        else if(jb[18].equals(actionEvent.getSource())){//"*"
            jtf.setText(jtf.getText()+"*");
        }
        else if(jb[13].equals(actionEvent.getSource())){//"/"
            jtf.setText(jtf.getText()+"/");
        }
        else if(jb[14].equals(actionEvent.getSource())){//"%"
            jtf.setText(jtf.getText()+"%");
        }
        else if(jb[24].equals(actionEvent.getSource())){//"="
            //"="按钮触发的事件为计算表达式，所以先判断先判断做哪种运算
            if(jtf.getText().indexOf('+')!=-1){//判断表达式中有无"+"
                String []s=jtf.getText().split("[+]");//将表达式以“+”为界限拆分成两部分
                Double num1=Double.parseDouble(s[0]);//将字符串转化成数值型
                Double num2=Double.parseDouble(s[1]);
                String ans=String.valueOf(num1+num2);//做加法后数值型转化为字符串型
                jtf.setText(ans);//输出到文本框
            }
            else if(jtf.getText().indexOf('-')!=-1){//"-"
                String []s=jtf.getText().split("[-]");
                Double num1=Double.parseDouble(s[0]);
                Double num2=Double.parseDouble(s[1]);
                String ans=String.valueOf(num1-num2);
                jtf.setText(ans);
            }
            else if(jtf.getText().indexOf('*')!=-1){//"*"
                String []s=jtf.getText().split("[*]");
                Double num1=Double.parseDouble(s[0]);
                Double num2=Double.parseDouble(s[1]);
                String ans=String.valueOf(num1*num2);
                jtf.setText(ans);
            }
            else if(jtf.getText().indexOf('/')!=-1){//"/"
                String []s=jtf.getText().split("[/]");
                Double num1=Double.parseDouble(s[0]);
                Double num2=Double.parseDouble(s[1]);
                double ans=num1/num2;
                jtf.setText(String.valueOf(num1/num2));
            }
            else if(jtf.getText().indexOf('%')!=-1){//"%"
                String []s=jtf.getText().split("[%]");
                Double num1=Double.parseDouble(s[0]);
                Double num2=Double.parseDouble(s[1]);
                double ans=num1%num2;
                jtf.setText(String.valueOf(num1%num2));
            }
        }
        else if(jb[19].equals(actionEvent.getSource())){//"1/x"求倒数
            Double num=Double.parseDouble(jtf.getText());
            jtf.setText(String.valueOf(1.0/num));
        }
        else if(jb[9].equals(actionEvent.getSource())){//"√"开根号
            Double num=Double.parseDouble(jtf.getText());
            jtf.setText(String.valueOf(Math.sqrt(num)));
        }
        else if(jb[7].equals(actionEvent.getSource())){//"C"清除文本区
            jtf.setText("0");
        }
        else if(jb[6].equals(actionEvent.getSource())){//"CE"清除文本区的最后一个字符
            String s=jtf.getText().substring(0,jtf.getText().length()-1);
            jtf.setText(s);
        }
        else if(jb[2].equals(actionEvent.getSource())){//"MS"存储当前的结果到存储区
            saved_area=jtf.getText();
        }
        else if(jb[0].equals(actionEvent.getSource())){//"MC"清除存储区
            saved_area="";
        }
        else if(jb[1].equals(actionEvent.getSource())){//"MR"显示存储区的值
            jtf.setText(saved_area);
        }
        else if(jb[3].equals(actionEvent.getSource())){//"M+"存储区中的值加上文本框中的值存储到存储区
            Double ans=Double.valueOf(saved_area)+Double.valueOf(jtf.getText());
            saved_area=String.valueOf(ans);
        }
        else if(jb[4].equals(actionEvent.getSource())){//"M-"
            Double ans=Double.valueOf(saved_area)-Double.valueOf(jtf.getText());
            saved_area=String.valueOf(ans);
        }
        else if(jb[8].equals(actionEvent.getSource())){//"±"求相反数
            Double ans=Double.valueOf(jtf.getText())*(-1);
            jtf.setText(String.valueOf(ans));
        }
    }
}
